#include <cstdio>
#include <algorithm>

using namespace std;

struct Edge
{
    int x, y, cost;
};

bool operator < ( const Edge &a, const Edge &b )
{
    return a.cost < b.cost;
}

Edge edge[ 80000 ];
int e, sum, n, s[ 110 ], n2;

void unionset( int root1, int root2 )
{
        s[ root1 ] = root2;
}

int find( int x )
{
    if ( s[ x ] < 0 )
        return x;
    else
        return s[ x ] = find( s[ x ] );
}

void Kruscal( )
{
    int i, j, v1, v2;
    sum = i = j = 0;
    while ( j < n2 && i < e )
    {
        v1 = find( edge[ i ].x );
        v2 = find( edge[ i ].y );
        if ( v1 != v2 )
        {
            sum += edge[ i ].cost;
            unionset( v1, v2 );
            j++;
        }
        i++;
    }
}

void print( )
{
    printf("%d\n", sum);
}

int main( )
{
    int i, j, a, b, c, d;
    while ( scanf("%d", &n) != EOF && n )
    {
        n2 = n;
        e = 0;
        for ( i = 1; i <= n; i++ )
            s[ i ] = -1;
        for ( i = 0; i < n * ( n - 1 ) / 2; i++ )
        {
            scanf("%d%d%d%d", &a, &b, &c, &d);
            if ( d )
            {
                a = find( a );
                b = find( b );
                if ( a != b )
                {
                    n2--;
                    unionset( a, b );
                }
            }
            else
            {
                edge[ e ].x = a;
                edge[ e ].y = b;
                edge[ e ].cost = c;
                e++;
            }
        }
        sort( edge, edge + e );
        Kruscal( );
        print( );
    }
    return 0;
}
